home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / frogbean / BeanDumper.java next >
Encoding:
Java Source  |  2000-05-25  |  5.3 KB  |  142 lines

  1. //: BeanDumper.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // A method to introspect a Bean
  50. import java.beans.*;
  51. import java.lang.reflect.*;
  52.  
  53. public class BeanDumper {
  54.   public static void dump(Class bean){
  55.     BeanInfo bi = null;
  56.     try {
  57.       bi = Introspector.getBeanInfo(
  58.         bean, java.lang.Object.class);
  59.     } catch(IntrospectionException ex) {
  60.       System.out.println("Couldn't introspect " +
  61.         bean.getName());
  62.       System.exit(1);
  63.     }
  64.     PropertyDescriptor[] properties = 
  65.       bi.getPropertyDescriptors();
  66.     for(int i = 0; i < properties.length; i++) {
  67.       Class p = properties[i].getPropertyType();
  68.       System.out.println(
  69.         "Property type:\n  " + p.getName());
  70.       System.out.println(
  71.         "Property name:\n  " + 
  72.         properties[i].getName());
  73.       Method readMethod = 
  74.         properties[i].getReadMethod();
  75.       if(readMethod != null)
  76.         System.out.println(
  77.           "Read method:\n  " + 
  78.           readMethod.toString());
  79.       Method writeMethod = 
  80.         properties[i].getWriteMethod();
  81.       if(writeMethod != null)
  82.         System.out.println(
  83.           "Write method:\n  " +
  84.           writeMethod.toString());
  85.       System.out.println("====================");
  86.     }
  87.     System.out.println("Public methods:");
  88.     MethodDescriptor[] methods =
  89.       bi.getMethodDescriptors();
  90.     for(int i = 0; i < methods.length; i++)
  91.       System.out.println(
  92.         methods[i].getMethod().toString());
  93.     System.out.println("======================");
  94.     System.out.println("Event support:");
  95.     EventSetDescriptor[] events = 
  96.       bi.getEventSetDescriptors();
  97.     for(int i = 0; i < events.length; i++) {
  98.       System.out.println("Listener type:\n  " +
  99.         events[i].getListenerType().getName());
  100.       Method[] lm = 
  101.         events[i].getListenerMethods();
  102.       for(int j = 0; j < lm.length; j++)
  103.         System.out.println(
  104.           "Listener method:\n  " +
  105.           lm[j].getName());
  106.       MethodDescriptor[] lmd = 
  107.         events[i].getListenerMethodDescriptors();
  108.       for(int j = 0; j < lmd.length; j++)
  109.         System.out.println(
  110.           "Method descriptor:\n  " +
  111.           lmd[j].getMethod().toString());
  112.       Method addListener = 
  113.         events[i].getAddListenerMethod();
  114.       System.out.println(
  115.           "Add Listener Method:\n  " +
  116.         addListener.toString());
  117.       Method removeListener =
  118.         events[i].getRemoveListenerMethod();
  119.       System.out.println(
  120.         "Remove Listener Method:\n  " +
  121.         removeListener.toString());
  122.       System.out.println("====================");
  123.     }
  124.   }
  125.   // Dump the class of your choice:
  126.   public static void main(String[] args) {
  127.     if(args.length < 1) {
  128.       System.err.println("usage: \n" +
  129.         "BeanDumper fully.qualified.class");
  130.       System.exit(0);
  131.     }
  132.     Class c = null;
  133.     try {
  134.       c = Class.forName(args[0]);
  135.     } catch(ClassNotFoundException ex) {
  136.       System.err.println(
  137.         "Couldn't find " + args[0]);
  138.       System.exit(0);
  139.     }
  140.     dump(c);
  141.   }
  142. } ///:~